//
//  GroupListViewController.m
//  17DianYing
//
//  Created by apple on 15/9/23.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "GroupListViewController.h"
#import "GetGroupListManager.h"
#import "UITableViewCell+Func.h"

@interface GroupListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *groupListTableView;

@property (nonatomic,strong)GetGroupListManager *groupListM;

@property (strong, nonatomic)NSMutableArray * cover_arr;

@property(strong,nonatomic)NSMutableArray *covergroup;

@end

@implementation GroupListViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];

    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.cover_arr = [NSMutableArray array];
    self.covergroup = [NSMutableArray array];
    
    UIImage *image = [UIImage imageNamed:@"矩形-2@3x.png"];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:image];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.groupListTableView.estimatedRowHeight = 80;
    
    
    //2.开启自动行高
    self.groupListTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.groupListM = [GetGroupListManager share];
    
    self.groupListTableView.delegate = self;
    self.groupListTableView.dataSource = self;
    
    self.groupListTableView.tableFooterView = [[UIView alloc]init];
    
     self.cover_arr = [NSMutableArray array];
    
    [self getgroupList];
    
    
    
}

-(void)getgroupList
{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    [self.groupListM getListWithToken:usertoken BlockHandle:^(NSDictionary *dic) {
        
        NSArray *arr = dic[@"groups"];
        for (NSDictionary *dicList in arr)
        {
            [self.cover_arr addObject:dicList];
        }
        
        [self.groupListTableView reloadData];
        
        [self.covergroup addObjectsFromArray:[[EaseMob sharedInstance].chatManager loadAllConversationsFromDatabaseWithAppend2Chat:YES]];
        
        [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
            if (!error) {
                
                
                [self.covergroup addObjectsFromArray:groups];
                
            }
        } onQueue:nil];

        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)tupAddButton:(id)sender
{
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    
    return self.cover_arr.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"coverCell" forIndexPath:indexPath];
    NSDictionary *dic = self.cover_arr[indexPath.row];

    [cell setCellInfoWithNSDictionary:dic];
    
//        EMConversation * conver = self.cover_arr[indexPath.row];
    
    
    //     [self.groupName setTitle:conver.chatter forState:0];
    NSLog(@"=========%@",self.cover_arr);
    
    
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    
    return YES;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
    
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EMConversation * conver = self.cover_arr[indexPath.row];
    
    BOOL delete = [[EaseMob sharedInstance].chatManager removeConversationByChatter:conver.chatter deleteMessages:YES append2Chat:NO];
    
    if (delete)
    {
        
        [self getgroupList];
        
    }
    else
    {
        
        [self showHint:@"删除失败"];
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMConversation * conversantion = self.covergroup[indexPath.row];
    NSDictionary *charDic = self.cover_arr[indexPath.row];
    
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController * vc = [story instantiateViewControllerWithIdentifier:@"chat"];
    
    
    [vc setValue:conversantion forKey:@"conversation"];
    [vc setValue:charDic forKey:@"charDic"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
